package tushu.Bean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.TreeSet;

import org.junit.jupiter.api.Test;

public class tushu {

	private static List<BookBean> booklist = new ArrayList<BookBean>();
	private static List<RentCardBean> cardlist = new ArrayList<RentCardBean>();
	private static List<BookRentJiLuBean> bookrentlist = new ArrayList<BookRentJiLuBean>();

	public static void main(String[] args) {
		while (true) {
			System.out.println("1.进书");
			System.out.println("2.借书卡办理");
			System.out.println("3.借书");
			System.out.println("4.还书");
			System.out.println("5.查询某书借出记录");
			System.out.println("6.查询借书卡的借出记录");
			System.out.println("7.显示图书列表(按借出次数排序)");
			tushu t = new tushu();
			t.caozuo();
		}
	}

	public void caozuo() {
		Scanner sc = new Scanner(System.in);
		String xuanze = sc.nextLine();
		switch (xuanze) {
		case "1":
			jinshu();
			break;
		case "2":
			cardbanli();
			break;
		case "3":
			jieshu();
			break;
		case "4":
			huanshu();
			break;
		case "5":
			jiluByBook();
			break;
		case "6":
			jiluByCard();
			break;
		case "7":
			listBook();
			break;

		default:
			break;
		}
	}

	@Test
	// public
	public void jinshu() {
		BookBean book = new BookBean();
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入进书图书id");
		int id = Integer.parseInt(sc.nextLine());
		System.out.println("请输入进书图书名称");
		String name = sc.nextLine();
		System.out.println("请输入进书图书数量");
		int number = Integer.parseInt(sc.nextLine());
		System.out.println("请输入进书图书放置区域");
		String address = sc.nextLine();
		book.setId(id);
		book.setBookName(name);
		book.setNumber(number);
		book.setAddress(address);
		booklist.add(book);
		for (BookBean bookBean : booklist) {
			System.out.println(bookBean.toString());
		}
	}

	/**
	 * 
	 */
	@Test
	public void cardbanli() {

		RentCardBean card = new RentCardBean();
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的卡号");
		int cardId = Integer.parseInt(sc.nextLine());
		System.out.println("请输入你的姓名");
		String name = sc.nextLine();
		// 将借书卡的信息保存在集合
		card.setId(cardId);
		card.setStudentName(name);
		cardlist.add(card);
		for (RentCardBean cardBean : cardlist) {
			System.out.println(cardBean.toString());
		}
//		System.out.println(cardlist.get(0));
	}

	@Test
	public void jieshu() {
		// 一个要改借书卡信息，一个改图书借出次数信息
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的借书卡号");
		Integer cardId = Integer.parseInt(sc.nextLine());
		System.out.println("请输入你借的书名");
		String bookName = sc.nextLine();
		// 获取本地时间
		Date nowtime = new Date();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = fmt.format(nowtime);

		int flag = 0;
		for (int i = 0; i < booklist.size(); i++) {
			BookBean book = booklist.get(i);
			if (bookName.equals(book.getBookName()) && book.getNumber() > 0) {
				flag = 1;
				break;
			} else {
				flag = 0;
			}
		}
		if (flag == 1) {
			System.out.println("借书成功");
		} else {
			System.out.println("图书不存在或库存不足");
		}

		for (int i = 0; i < cardlist.size(); i++) {
			// 专门提取出来cardBean方便进行修改内部信息
			RentCardBean card = cardlist.get(i);
			Integer id = new Integer(card.getId());
			if (id.equals(cardId)) {
				// 这里更新某书的接触记录Bean
				BookRentJiLuBean bookrent = new BookRentJiLuBean();
				bookrent.setName(bookName);
				bookrent.setCardId(cardId);
				Integer n = bookrent.getRentCiShu();
				if (n == null) {
					n = 0;
				}
				bookrent.setRentCiShu(n + 1);
				bookrent.setRentPerson(card.getStudentName());
				bookrent.setTime(time);
				bookrentlist.add(bookrent);
			}
			// 下面使用的Integer.valueOf()方法
//			if(Integer.valueOf(cardlist.get(i).getId()).equals(cardId)) {
//			}
		}

		for (int i = 0; i < booklist.size(); i++) {
			BookBean book = booklist.get(i);
			int cishu = book.getRentCiShu();
			int bookNumber = book.getNumber();
			if (bookNumber > 0) {
				if (book.getBookName().equals(bookName)) {
					book.setRentCiShu(cishu + 1);
					book.setNumber(bookNumber - 1);
				}
			}
		}
//		// 测试
//		System.out.println(booklist.get(0).toString());
//		// 测试
//		System.out.println(cardlist.get(0).toString());
	}
// BookBean number   BookRentJiLuBean guiHuanTime
	public void huanshu() {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你还书的书名");
		String bookName = sc.nextLine();
		System.out.println("请输入你的借书卡号");
		Integer cardId = Integer.parseInt(sc.nextLine());
		
		Date time = Calendar.getInstance().getTime();
		SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		String fmtTime = fmt.format(time);
		
		//书名 卡号对应 修改 库存量 还书时间
		for (int i = 0; i < booklist.size(); i++) {
			BookBean book = booklist.get(i);
			BookRentJiLuBean rentBean = lookByIdName(bookName, cardId);
			if (rentBean==null) {
				break;
			}
			if (book.getBookName().equals(bookName)) {
				book.setNumber(book.getNumber() + 1);
				rentBean.setGuiHuanTime(fmtTime);
			}
		}
		// 测试
//		System.out.println(booklist.get(0).toString());
	}

	/**
	 * @return
	 */
	public BookRentJiLuBean lookByIdName(String name,int id) {
		int flag=0;
		BookRentJiLuBean RentJiLu = null;
		for (int i = 0; i <bookrentlist.size() ; i++) {
			RentJiLu = bookrentlist.get(i);
			if (RentJiLu.getCardId()==id&&RentJiLu.getName().equals(name)) {
				flag=1;
				System.out.println("还书成功");
				break;
			}
		}
		if (flag==0) {
			System.out.println("输入的书名或卡号不正确");
		}
		 return RentJiLu;
	}
	public void jiluByBook() {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入查询某一本书借出记录的书名：");
		String bookName = sc.nextLine();
		for (int i = 0; i < bookrentlist.size(); i++) {
			BookRentJiLuBean bookrent = bookrentlist.get(i);
			if (bookrent.getName().equals(bookName)) {
				System.out.println(bookrent.toString());
			}
			// 全部书的借出记录
//			System.out.println(bookrentlist.get(i).toString());
		}
	}

	public void jiluByCard() {
		Scanner sc = new Scanner(System.in);
		System.out.println("输入查询借书卡的借出记录的卡号：");
		Integer cardId = Integer.parseInt(sc.nextLine());
		BookRentJiLuBean cardRent=null;
		for (int i = 0; i < bookrentlist.size(); i++) {
			cardRent = bookrentlist.get(i);
			if (cardRent.getCardId()==cardId){
				System.out.println(cardRent.toString());
			}
			
			// 输出全部借书卡的借出记录
//			System.out.println(cardlist.get(i).toString());
		}

	}

	public void listBook() {
		Collections.sort(bookrentlist, new Comparator<BookRentJiLuBean>() {

			@Override
			public int compare(BookRentJiLuBean o1, BookRentJiLuBean o2) {
				return o2.getRentCiShu() - o1.getRentCiShu();
			}

		});
		for (int i = 0; i < booklist.size(); i++) {
			BookBean book = booklist.get(i);
			System.out.println(book.toString());
		}

	}
}
